package com.sjy.mvvmdemo.roomdb.dbfactory

import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.sjy.mvvmdemo.roomdb.SecondViewModel

/**
 * db 和 vm绑定的factory
 */
class UserViewModelFactory(private val userRepository: UserRepository) : ViewModelProvider.Factory {
    override fun <T : ViewModel?> create(modelClass: Class<T>): T {
        //绑定vm
        if (modelClass.isAssignableFrom(SecondViewModel::class.java)) {
            return SecondViewModel(userRepository) as T
        } else {
            throw IllegalArgumentException("Unknown View Model class")
        }
    }
}